package com.inspur.cmis.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.inspur.cmis.pojo.User;
import com.inspur.cmis.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("/userInfoList")
	public String userInfoList(Model model,HttpServletRequest request,@RequestParam(value="pn",defaultValue="1") Integer pn){
		String userid=request.getParameter("userId");
		String userName=request.getParameter("username");
		Map map=new HashMap<String,String>();
		map.put("userId",userid);
		map.put("userName", userName);
		
		PageHelper.startPage(pn,2);
		List<User> userList = userService.getUserList(map);
		PageInfo<User> pageinfo=new PageInfo<User>(userList,userList.size());		
		//model.addAttribute("userList", userList);
		model.addAttribute("pageinfo", pageinfo);
		model.addAttribute("userId",userid);
		model.addAttribute("userName",userName);
		for(int i=0;i<userList.size();i++)
		{System.out.println(userList.get(i).toString());		
		}
		return "/user/userInfo.jsp";
	}
	@RequestMapping("/deleteUser")
	public String DeleteUser(String userIds) {		
		String usersId[]=userIds.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    userService.deleteUser(user_ids);
	  return "redirect:userInfoList";
	}
	@RequestMapping("/UserOpen")
	public String UserOpen(String userIds) {		
		String usersId[]=userIds.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    userService.UserOpen(user_ids);
	  return "redirect:userInfoList";
	}
	@RequestMapping("/UserClose")
	public String UserClose(String userIds) {		
		String usersId[]=userIds.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    userService.UserClose(user_ids);
	  return "redirect:userInfoList";
	}
	@RequestMapping("/UserReset")
	public String UserReset(String userIds) {
		
		String usersId[]=userIds.split(",");
		int user_ids[]=new int[usersId.length];
	    for(int i=0;i<usersId.length;i++)
	    {user_ids[i]=Integer.parseInt(usersId[i]);}
	    userService.UserReset(user_ids);
	  return "redirect:userInfoList";
	}
	@RequestMapping("/useInfoAdd")
	public String userAdd(User user,Model model){
		Date today = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String time =format.format(today);
		user.setRegisterTime(time);
		user.setLastTime(time);
		user.setIsEnable("T");
		user.setAnswer(user.getPassword());
		
		userService.addUser(user);
		
		model.addAttribute("success", "保存成功");
		model.addAttribute("member",user);
		model.addAttribute("flag", "1");
		
		return "/user/userInfoAdd.jsp";
		
	}
	
	@RequestMapping("/checkUser")
	@ResponseBody //controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON数据或者是XML 数据,...
	public Map checkUser(String username) {
		System.out.println("前台Ajax传递过来的参数: " +username);
		List<User> selectUser = userService.getUserByUserName(username);
		Map map = new HashMap();
		map.put("selectUser", selectUser);
		if (selectUser.size() >= 1) {
			map.put("msg", "已存在该用户");
		}
		System.out.println("map==="+map);
		return map;
	}
	@RequestMapping("/modifyUser")
	public String modifyUser(String userId,Model model){
	  int user_id = Integer.parseInt(userId);
	  User user = userService.getUserById(user_id);
	  model.addAttribute("member", user);
	  return "/user/userInfoUpdate.jsp";
	}
	

	@RequestMapping("/userInfoUpdate")
	public String userupdate(User user,Model model){
		
		Date today = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String time =format.format(today);
		user.setLastTime(time);
		
		userService.updateUser(user);
		
		model.addAttribute("success", "修改成功");
		model.addAttribute("member",user);
		model.addAttribute("flag", "1");
		
		return "/user/userInfoUpdate.jsp";
		
	}
}
	//@RequestMapping("/userInfoList")
	//public String userInfoList(Model model){
	//	List<User> userList = userService.getAllUserList();
	//	model.addAttribute("pageinfo", userList);
	//	return "/user/userInfo.jsp";
	//}

